package edu.umd.ion.wavemeter.server;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;

public class LogTemperature {
	EntityManagerFactory MyEntityManagerFactory =  WavemeterConfiguration.createEntityManagerFactory();
	ExecutorService MyWorkQueue = Executors.newSingleThreadExecutor();

	private class LogTemperatureTask implements Runnable {
		double T;
		Date When;
		public LogTemperatureTask(double temp) {
			T = temp;
			When = new Date( System.currentTimeMillis());
		}
		public void run() {
			EntityManager em = MyEntityManagerFactory.createEntityManager();
			EntityTransaction userTransaction = em.getTransaction();

			userTransaction.begin();
			PersistentTemperature Temp = new PersistentTemperature();
			Temp.temperature = T;
			Temp.time = When;
			em.persist(Temp);
			userTransaction.commit();
			em.close();	
		}	
	}

	public void logTemperature(double temp ) {
		MyWorkQueue.execute( new LogTemperatureTask(temp) );
	}
}
